<!--
 * @Description: 基本信息
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2021-12-27 10:27:09
 * @LastEditTime: 2023-06-22 17:39:35
 * @FilePath: \cscec-pms-admin\src\views\construction\monthPlanAdjust\components\BasicInfo.vue
-->
<template>
  <custom-form
    ref="form"
    :is-validate="true"
    :edit-model="''"
    :rules="rules"
    :form="form"
    :form-list="formOption"
  >
    <template slot="projectId" 
      slot-scope="{data:{formItem:{disabled}}}">
      <org-tree-select
        v-model="form.projectId"
        is-need-auth
        is-auto-select-first
        check-strictly
        :disabled="disabled"
        @select="selectOrg"
      />
    </template>
  </custom-form>
</template>
<script>
import { formOption } from '../model.js'
import { view } from '@/api/construction/monthPlanAdjust'
export default {
  name: 'BasicInfo',
  props: {
    isAudit: {
      // 是否作为审批详情
      type: Boolean,
      default: false
    },
    // 加载
    loading: {
      type: Boolean,
      require: true
    }
  },
  components: {},
  data() {
    return {
      // 表单
      form: {
        projectId: void 0,
        belongMonth: void 0,
        adjustReason: void 0,
        adjustMaxLevel: void 0,
        situationExplain: void 0
      },
      // 调整原因
      reasonList: [],
    }
  },
  computed: {
    // 是查看
    isDetail() {
      return this.$route.query.pageType === 'detail' || this.isAudit
    },
    // 表单项
    formOption() {
      this.$checkRequireItem()
      let list = this.$clone(formOption)
      if (this.adjustId !== '') {
        list[0].disabled = true
      } else {
        list[0].disabled = false
      }
      list[2].data = this.reasonList
      if (this.isDetail) {
        list = list.map((el) => {
          const item = this.$clone(el)
          item.disabled = true
          return item
        })
      }
      return list
    },
    // 表单校验
    rules() {
      const obj = {}
      formOption.map((item) => {
        if (item.rules) {
          obj[item.keyName] = item.rules
        }
      })
      return obj
    },
    // 调整id
    adjustId() {
      return this.$route.query.id ?? ''
    }
  },
  async mounted() {
    //已经有id需要获取基本信息
    if (this.adjustId !== '') {
      this.$emit('update:loading', true)
      this.getBasisInfo()
    }else{
      // 新增时设置所属月度
      const date = new Date()
      this.form.belongMonth = date.getFullYear() + '年'+(date.getMonth()+1 < 10 ? '0' + (date.getMonth()+1): date.getMonth()+1)+'月'
    }
    this.getReason()
  },
  methods: {
    selectOrg(val) {
      if(val.length > 1) {
        this.form.branchComId = val[val.length - 2].orgId
        this.form.projectId = val[val.length - 1].projectId
      }
    },
    /**
     * @description: 格式化时间
     * @param {object/string} date 时间对象或字符串
     * @param {boolean} toMoment 是否转换为Moment
     * @return {object} moment时间格式
     */
    formatDate(date, toMoment = true) {
      if (date?._isAMomentObject) {
        return toMoment ? date : date.format('YYYY-MM-DD')
      } else {
        return toMoment ? this.moment(date) : date
      }
    },
    /**
     * @description: 获取调整原因
     * @param {void}
     * @return {void}
     */
    getReason() {
      view.planLabel({ type: '12' }).then(({ code, data }) => {
        if (code === 200) {
          this.reasonList = data.map((el) => {
            return {
              value: el.id,
              label: el.name
            }
          })
        } else {
          this.reasonList = []
        }
      })
    },
    /**
     * @description: 获取基本信息
     * @param {void}
     * @return {void}
     */
    getBasisInfo() {
      view
        .basisDetail(this.adjustId)
        .then(({ code, data }) => {
          if (code === 200) {
            for (let item in this.form) {
              this.form[item] = data[item] || void 0
            }
          }
        })
        .finally(() => {
          this.$emit('update:loading', false)
        })
    },
    /**
     * @description: 提交
     * @param {void}
     * @return {void}
     */
    handleSubmit() {
      return new Promise((resolve, reject) => {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$emit('update:loading', true)
            const { projectId, adjustReason, situationExplain } = this.form
            if (!this.adjustId) {
              // 新增
              view
                .createBasis({
                  projectId,
                  adjustReason,
                  situationExplain
                })
                .then(({ code, data }) => {
                  if (code === 200) {
                    this.$message.success('新增基本信息成功')
                    this.$router.replace({
                      query: {
                        pageType: this.$route.query.pageType,
                        id: data
                      }
                    })
                    resolve()
                  } else {
                    reject()
                  }
                })
                .catch((error) => {
                  reject()
                })
                .finally(() => {
                  this.$emit('update:loading', false)
                })
            } else {
              // 查看
              if (this.isDetail) {
                this.$emit('update:loading', false)
                resolve()
                return
              }
              // 编辑
              const { projectId, adjustReason, situationExplain } = this.form
              view
                .editBasis({
                  id: this.adjustId,
                  projectId,
                  adjustReason,
                  situationExplain
                })
                .then(({ code, data }) => {
                  if (code === 200) {
                    if (!this.isDetail) {
                      this.$message.success('编辑基本信息成功')
                    }
                    resolve()
                  } else {
                    reject()
                  }
                })
                .catch((error) => {
                  reject()
                })
                .finally(() => {
                  this.$emit('update:loading', false)
                })
            }
          } else {
            reject()
          }
        })
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
